
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Configuration;
using System.Diagnostics;

namespace Swaf.Instrumentation
{
	/// <summary>
	/// Summary description for AverageCounter.
	/// </summary>
	public class AverageCounter : BaseCounter
	{
		protected PerformanceCounter m_baseCounter;

		public AverageCounter(IApplication app, string name) 
			: base( app,name)
		{
		}
		public override bool createCounters(CounterCreationDataCollection ccdc)
		{
			// Add the general avg.
			m_counterCreated=!counterExists(m_counterName,m_categoryName);
			CounterCreationData averageGeneral = new CounterCreationData();
			averageGeneral.CounterType = PerformanceCounterType.AverageTimer32;
			averageGeneral.CounterName = name;
			ccdc.Add(averageGeneral);

			averageGeneral = new CounterCreationData();
			averageGeneral.CounterType = PerformanceCounterType.AverageBase;
			averageGeneral.CounterName = name + "Base";
			ccdc.Add(averageGeneral);
			return m_counterCreated;
		}
		public override void initCounters()
		{
			PerformanceCounter p = new PerformanceCounter(m_categoryName, m_counterName, false);
			p.RawValue = 0;
			m_primaryCounter = p;

			p = new PerformanceCounter(m_categoryName, m_counterName + "Base", false);
			p.RawValue = 0;
			m_baseCounter = p;
		}
		public override void increment()
		{
			throw new CounterException("AverageCounters can't be incremented without providing an increment value");
			//base.increment ();
		}
		public override void decrement()
		{
			throw new CounterException("AverageCounters can't be decremented");
		}
		public override void decrementBy(long decValue)
		{
			throw new CounterException("AverageCounters can't be decremented");
		}
		public override void incrementBy(long incValue)
		{
			lock(m_lockValue)
			{
				m_primaryCounter.IncrementBy(incValue);
				m_baseCounter.Increment();
			}
		}





	}
}
